package com.haier.utils;

import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiGettokenRequest;
import com.dingtalk.api.request.OapiUserGetRequest;
import com.dingtalk.api.request.OapiUserGetuserinfoRequest;
import com.dingtalk.api.response.OapiGettokenResponse;
import com.dingtalk.api.response.OapiUserGetResponse;
import com.dingtalk.api.response.OapiUserGetuserinfoResponse;
import com.haier.constant.Constant;
import com.haier.constant.URLConstant;

public class H5Util {

	public static String getToken() throws Exception {
		DefaultDingTalkClient client = new DefaultDingTalkClient(URLConstant.URL_GET_TOKKEN);
		OapiGettokenRequest request = new OapiGettokenRequest();

		request.setAppkey(Constant.H5_APP_KEY);
		request.setAppsecret(Constant.H5_APP_SECRET);
		request.setHttpMethod("GET");
		OapiGettokenResponse response = client.execute(request);
		String accessToken = response.getAccessToken();
		return accessToken;
	}

	public static OapiUserGetuserinfoResponse getUserId(String authCode, String accessToken) throws Exception {
		DingTalkClient client = new DefaultDingTalkClient(URLConstant.URL_GET_USER_INFO);
		OapiUserGetuserinfoRequest request = new OapiUserGetuserinfoRequest();
		request.setCode(authCode);
		request.setHttpMethod("GET");
		OapiUserGetuserinfoResponse response = client.execute(request, accessToken);
		return response;
	}

	public static OapiUserGetResponse getInfo(String accessToken, String userId) throws Exception {
		DingTalkClient client = new DefaultDingTalkClient(URLConstant.URL_USER_GET);
		OapiUserGetRequest request = new OapiUserGetRequest();
		request.setUserid(userId);
		request.setHttpMethod("GET");
		OapiUserGetResponse response = client.execute(request, accessToken);
		return response;
	}
}
